/**
 * 表单验证组合式函数
 * 提供表单验证相关的通用方法
 */
export function useFormValidation() {
  const validateForm = async (formRef) => {
    if (!formRef) {
      throw new Error('Form reference is required');
    }

    try {
      await formRef.validate();
      return true;
    } catch (error) {
      // 创建一个自定义错误类型，便于在调用处识别
      const validationError = new Error('Form validation failed');
      validationError.name = 'ValidationError';
      validationError.originalError = error;
      throw validationError;
    }
  };

  const clearValidation = (formRef) => {
    if (formRef) {
      formRef.clearValidate();
    }
  };

  const resetFields = (formRef) => {
    if (formRef) {
      formRef.resetFields();
    }
  };

  return {
    validateForm,
    clearValidation,
    resetFields,
  };
}
